package net.expectx.micro.shop.controller.product;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.expectx.common.single.base.BaseResult;
import net.expectx.common.single.util.Md5Util;
import net.expectx.micro.shop.common.AdminConstants;
import net.expectx.micro.shop.common.AdminResult;
import net.expectx.micro.shop.common.util.ExpectXUtil;
import net.expectx.micro.shop.controller.ManagerController;
import net.expectx.micro.shop.dao.bean.PermissionUser;
import net.expectx.micro.shop.dao.bean.PermissionUserExample;
import net.expectx.micro.shop.dao.bean.ProductBrand;
import net.expectx.micro.shop.dao.bean.ProductBrandExample;
import net.expectx.micro.shop.rpc.api.PermissionUserService;
import net.expectx.micro.shop.rpc.api.ProductBrandService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author hxs
 */

@Controller
@RequestMapping("/manager/productBrand")
@Api(value = "产品品牌", description = "产品品牌")
public class ProductBrandController {
    private static final Logger LOGGER = Logger.getLogger(ManagerController.class);

    @Autowired
    ProductBrandService productBrandService;

    @ApiOperation(value = "产品品牌首页")
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(ModelMap modelMap) {
        return "product/brand/index";
    }

    @ApiOperation(value = "新增产品品牌")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        return "product/brand/modify";
    }

    @ApiOperation(value = "修改产品品牌")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        ProductBrand productBrand = productBrandService.selectByPrimaryKey(id);
        modelMap.put("productBrand", productBrand);
        return "product/brand/modify";
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Object page(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request,String productBrandName) {
        Map<String, Object> result = new HashMap<>(16);
        ProductBrandExample productBrandExample = new ProductBrandExample();
        /**
         * 创造条件
         */
        ProductBrandExample.Criteria criteria = productBrandExample.createCriteria();

        if (StringUtils.isNotBlank(productBrandName)){
            criteria.andProductBrandNameLike("%"+productBrandName+"%");
        }
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        /**
         * 分页
         */
        productBrandExample.setPageIndex(page);
        productBrandExample.setPageSize(limit);
        /**
         * 排序
         */
        productBrandExample.setOrderByClause("gmt_modified desc");

        List<ProductBrand> productBrands = productBrandService.selectByExample(productBrandExample);

        long total = productBrandService.countByExample(productBrandExample);

        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", productBrands);
        return result;
    }

    @ApiOperation(value = "新增产品品牌")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(ProductBrand productBrand) {
        productBrand.setGmtCreate(System.currentTimeMillis());
        productBrand.setGmtModified(System.currentTimeMillis());
        productBrand.setIsDeleted((byte) 0);
        productBrand.setOrders(System.currentTimeMillis());
        return productBrandService.insertSelective(productBrand);
    }

    @ApiOperation(value = "修改产品品牌")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Long id, ProductBrand productBrand) {
        productBrand.setGmtModified(System.currentTimeMillis());
        return  productBrandService.updateByPrimaryKeySelective(productBrand);
    }

    @ApiOperation(value = "删除产品品牌")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object physicalRemove(@PathVariable("ids") String ids) {
        ProductBrandExample productBrandExample = new ProductBrandExample();
        productBrandExample.createCriteria().andIdIn(ExpectXUtil.toList(ids,","));
        BaseResult baseResult = productBrandService.physicalRemoveByExample(AdminConstants.YES, productBrandExample);
        return new AdminResult(baseResult);
    }
}
